
package org.hbhk.aili.job.configuration;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.List;

import org.hbhk.aili.job.annotation.EnableJob;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.util.StringUtils;

public class JobScannerRegistrar implements ImportBeanDefinitionRegistrar, ResourceLoaderAware, EnvironmentAware {
	private ResourceLoader resourceLoader;
	private Environment environment;

	@Override
	public void setEnvironment(Environment environment) {
		this.environment = environment;
	}

	@Override
	public void setResourceLoader(ResourceLoader resourceLoader) {
		this.resourceLoader = resourceLoader;
	}

	@Override
	public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
		AnnotationAttributes attributes = AnnotationAttributes
				.fromMap(importingClassMetadata.getAnnotationAttributes(EnableJob.class.getName()));
		ClassPathJobScanner scanner = new ClassPathJobScanner(registry);

		Class<? extends Annotation> aClass = attributes.annotationType();
		if (resourceLoader != null) {
			scanner.setResourceLoader(resourceLoader);
		}

		List<String> basePackages = new ArrayList<>();
		for (String pkg : attributes.getStringArray("value")) {
			if (StringUtils.hasText(pkg)) {
				basePackages.add(pkg);
			}
		}

		scanner.setEnvironment(environment);
		scanner.setResourceLoader(resourceLoader);

		scanner.registerFilters();
		scanner.doScan(StringUtils.toStringArray(basePackages));
	}
}
